/*
 * Created by JFormDesigner on Fri Apr 29 14:15:02 CST 2022
 */

package View.DrawPanel;

import java.awt.event.*;

import Model.DrawModel.CalPoint;
import Model.DrawModel.HistoricalTrack;
import View.DrawMode;
import View.UpdateAutoComponent;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.*;

/**
 * @author dn
 */
public class ShowPanel extends JPanel implements UpdateAutoComponent {
    //   存储绘图实体
    private Graphics g = super.getGraphics(); // 初始化继承父组件的绘制数据

    // 控制刷新线程运行
    private static volatile boolean Working = false;

    //点的坐标放大了N倍
    private int N = 15;
    //为了让点从屏幕中间出现，把每个点都移动了Move*Move个像素点
    private int XMove = 200;
    private int YMove = 200;
    
    
    //目标的"图像的边数"
    private int size;

    //刷新率
    private int sleepTime;

    // 是否显示历史轨迹
    private boolean showHistory = true;
    // 是否绘制船与船之间的连线
    private boolean showLines = true;
    // 是否绘制中心点
    private boolean showCenter = true;

    //目标队列坐标数据
    ArrayList<CalPoint> TargetList;
    //船队坐标数据
    ArrayList<CalPoint> ShipsList;
    //船队历史轨迹数据
    ArrayList<HistoricalTrack> HistoricalShipsList;
    //绘制虚拟目标数据
    ArrayList<CalPoint> VirtualTargetList;
    //目标中心点
    CalPoint p;

    public ShowPanel(
            ArrayList<CalPoint> TargetList,
            ArrayList<CalPoint> ShipsList,
            ArrayList<CalPoint> VirtualTargetList,
            CalPoint p,
            ArrayList<HistoricalTrack> HistoricalShipsList,
            int sleepTime
    ) {
        this.TargetList = TargetList;
        if (TargetList != null) this.size = TargetList.size();
        this.ShipsList = ShipsList;
        this.VirtualTargetList=VirtualTargetList;
        this.HistoricalShipsList = HistoricalShipsList;
        this.p = p;
        this.sleepTime = sleepTime;
        initComponents();
    }

    /**
     * 设置放大倍数和移动距离
     *
     * @param N    n
     * @param Move 移动
     */
    @Override
    public void setMultiplyAndMovement(int N, int Move){
        this.N = N;
        this.XMove = Move;
        this.YMove = Move;
    }

    /**
     * 繁殖和运动
     * 设置放大倍数和移动距离
     *
     * @param N     n
     * @param XMove xmove
     * @param YMove ymove
     */
    public void setMultiplyAndMovement(int N, int XMove, int YMove){
        this.N = N;
        this.XMove = XMove;
        this.YMove = YMove;
    }

    /**
     * 设置绘制模式
     * @param mode 模式
     */
    @Override
    public void setDrawPattern(DrawMode mode) {
        switch (mode) {
            case DRAW_SHIP_AND_TARGET_WITHOUT_HISTORY:
                this.showHistory = false;
                break;
            case DRAW_SHIP_AND_TARGET:
            default:
                this.showHistory = true;
                break;
        }
    }

    // 启动自动刷新组件
    @Override
    public void beginUpdateComponent() {
//        System.out.println("{ShowPanel}组件 自动刷新方法 被调用");
        Working = true;
        new Thread(new Runnable() {
            public void run() {
                while (Working) {
                    repaint();//调用paint方法，画出当前点集合的线
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
//                System.out.println("{ShowPanel}组件 自动刷新方法 新建线程运行结束");
            }
        }).start();
    }

    // 停止自动刷新组件
    @Override
    public void endUpdateComponent(){
//        System.out.println("{ShowPanel}组件 结束自动刷新方法 被调用");
        Working = false;
    };

    //设置船队坐标数组（带历史轨迹）
    public void setArray(ArrayList<CalPoint> TargetList,
                         ArrayList<HistoricalTrack> HistoricalShipsList,
                         CalPoint p) {
//        System.out.println("{ShowPanel}组件设置内容方法被调用" + HistoricalShipsList.size());
        this.TargetList = TargetList;
        this.HistoricalShipsList = HistoricalShipsList;
        this.p = p;
    }

    //画图
    public void paint(Graphics g) {
//        System.out.println("{ShowPanel}组件paint方法被调用");
//        paintComponent(g);
        this.g = g;
        // 清除所有数据，后续重新绘制组件
        g.clearRect(0, 0, this.getWidth(), this.getHeight());

        // 在这里重绘所有子组件，保证子组件不被clear
        this.menuBar.paint(g);

        // 在这里执行自定义的绘制
        if(TargetList != null) this.drawTargets(g);
        if(showCenter && this.p != null) this.drawTargetCenter(g);
        this.drawShips(g);
        if(VirtualTargetList!=null){ this.drawVirtualTargets(g);
        this.drawVirtualTargetsLine(g);}
        // 是否绘制船队连线
        if(showLines) this.drawShipsLine(g);
        // 是否绘制历史记录
        if (showHistory) this.drawHistory(g);
//        System.out.println("{ShowPanel}组件paint结束");
    }

    //绘制目标队列和中心点坐标
    private void drawTargets(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置画笔颜色
        g2d.setColor(Color.RED);
        // 绘制目标队列图
        for (int i = 0; i < TargetList.size(); i++) {
            // 绘制小船
            double x = TargetList.get(i).x * N + XMove;
            double y = TargetList.get(i).y * N + YMove;
            g2d.setColor(Color.BLACK);
            g2d.fillRect((int)x-4,(int)y-4, 8, 10);
//            g2d.setColor(Color.RED);
//            int[] xPoints = {(int)x-4, (int)x, (int)x+4};
//            int[] yPoints = {(int)y-4, (int)y-8, (int)y-4};
//            g.drawPolygon(xPoints, yPoints, 3);
            // 绘制连线
            if (showLines) {
                Line2D line = new Line2D.Double(TargetList.get(i).x * N + XMove, TargetList.get(i).y * N + YMove, TargetList.get((i + 1) % size).x * N + XMove, TargetList.get((i + 1) % size).y * N + YMove);
                g2d.draw(line);
            }
        }
        g2d.dispose();
    }

    private void drawTargetCenter(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置画笔颜色
        g2d.setColor(Color.RED);
         // 设置画笔颜色
        g2d.setColor(Color.GREEN);
        //绘制目标中心点
        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(p.x* N + XMove, p.y * N + YMove, p.x* N + XMove + 1.5 , p.y* N + YMove + 1.5);
        g2d.draw(circle);
        // 创建的副本用完销毁
        g2d.dispose();
    }

    //绘制船队
    private void drawShips(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 绘制小船
        for (int i = 0; i < ShipsList.size(); i++) {
            double x = ShipsList.get(i).x* N + XMove;
            double y = ShipsList.get(i).y * N + YMove;
//            g2d.setColor(Color.BLACK);
//            g2d.fillRect((int)x-4,(int)y-4, 8, 10);
            g2d.setColor(Color.GREEN);
//            int[] xPoints = {(int)x-4, (int)x, (int)x+4};
//            int[] yPoints = {(int)y-4, (int)y, (int)y-4};
//            g2d.fillPolygon(xPoints, yPoints, 3);
            g2d.fillOval((int)x-4,(int)y-4,10,10);
        }
        // 创建的副本用完销毁
        g2d.dispose();
    }

    //绘制船队的连线
    private void drawShipsLine(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置画笔颜色
        g2d.setColor(Color.RED);
        for (int i = 0; i < ShipsList.size(); i++) {
            Line2D line = new Line2D.Double();
            line.setLine(ShipsList.get(i).x* N + XMove, ShipsList.get(i).y * N + YMove,ShipsList.get( (i+1) % (ShipsList.size())).x* N + XMove, ShipsList.get( (i+1) % (ShipsList.size())).y* N + YMove);
            //circle.setFrameFromCenter(ShipsList.get(i).x* N + Move, ShipsList.get(i).y * N + Move, ShipsList.get(i).x* N + Move + 3 , ShipsList.get(i).y* N + Move + 3);
            g2d.draw(line);
        }
    }

    //绘制船队历史轨迹
    private void drawHistory(Graphics g) {
//        System.out.println("正在绘制历史轨迹");
        Graphics2D g2d = (Graphics2D) g.create();
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置画笔颜色
        g2d.setColor(Color.BLUE);
        // 绘制历史轨迹图
        for (int i = HistoricalShipsList.size() - 2; i >=0 ; i--) {
            HistoricalTrack h1 = HistoricalShipsList.get(i); //获取i时间的船的位置
            HistoricalTrack h2 = HistoricalShipsList.get(i+1); //获取i+1时间的船的位置
            for(int j = h1.HistoricalShips.size() - 1; j >=0 ; j--) {
                Line2D line = new Line2D.Double(h1.HistoricalShips.get(j).x * N + XMove, h1.HistoricalShips.get(j).y * N + YMove, h2.HistoricalShips.get(j).x * N + XMove, h2.HistoricalShips.get(j).y * N + YMove);
                g2d.draw(line);
            }
        }
        // 创建的副本用完销毁
        g2d.dispose();
    }

    //绘制虚拟坐标
    private void drawVirtualTargets(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 绘制小船
        for (int i = 0; i < VirtualTargetList.size(); i++) {
            double x = VirtualTargetList.get(i).x* N + XMove;
            double y = VirtualTargetList.get(i).y * N + YMove;
//            g2d.setColor(Color.BLACK);
//            g2d.fillRect((int)x-4,(int)y-4, 8, 10);
            g2d.setColor(Color.ORANGE);
            g2d.fillOval((int)x-4,(int)y-4,10,10);
        }
        // 创建的副本用完销毁
        g2d.dispose();
    }
    //绘制虚拟坐标连线
    private void drawVirtualTargetsLine(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置画笔颜色
        g2d.setColor(Color.RED);
        for (int i = 0; i < VirtualTargetList.size(); i++) {
            Line2D line = new Line2D.Double();
            line.setLine(VirtualTargetList.get(i).x* N + XMove, VirtualTargetList.get(i).y * N + YMove,VirtualTargetList.get( (i+1) % (VirtualTargetList.size())).x* N + XMove, VirtualTargetList.get( (i+1) % (VirtualTargetList.size())).y* N + YMove);
            //circle.setFrameFromCenter(ShipsList.get(i).x* N + Move, ShipsList.get(i).y * N + Move, ShipsList.get(i).x* N + Move + 3 , ShipsList.get(i).y* N + Move + 3);
            g2d.draw(line);
        }
    }
    /*
    *@ 以下为自动生成代码
    * */

    // 默认初始化方法， 自动生成
    public ShowPanel() {
        initComponents();
    }
    

    private void menuItem1MouseEntered(MouseEvent e) {
        // TODO add your code here
        showHistory = true;
    }

    private void menuItem2MouseEntered(MouseEvent e) {
        // TODO add your code here
        showHistory = false;
    }

    private void menuItem3MouseEntered(MouseEvent e) {
        // TODO add your code here
        showLines = true;

    }

    private void menuItem4MouseEntered(MouseEvent e) {
        // TODO add your code here
        showLines = false;
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        menuBar = new JMenuBar();
        menu3 = new JMenu();
        menuItem1 = new JMenuItem();
        menuItem2 = new JMenuItem();
        menu1 = new JMenu();
        menuItem3 = new JMenuItem();
        menuItem4 = new JMenuItem();

        //======== this ========
        setLayout(null);

        //======== menuBar ========
        {

            //======== menu3 ========
            {
                menu3.setText("History");

                //---- menuItem1 ----
                menuItem1.setText("show");
                menuItem1.setSelected(true);
                menuItem1.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        menuItem1MouseEntered(e);
                    }
                });
                menu3.add(menuItem1);

                //---- menuItem2 ----
                menuItem2.setText("hide");
                menuItem2.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        menuItem2MouseEntered(e);
                    }
                });
                menu3.add(menuItem2);
            }
            menuBar.add(menu3);

            //======== menu1 ========
            {
                menu1.setText("Ligature");

                //---- menuItem3 ----
                menuItem3.setText("show");
                menuItem3.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        menuItem3MouseEntered(e);
                    }
                });
                menu1.add(menuItem3);

                //---- menuItem4 ----
                menuItem4.setText("hide");
                menuItem4.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        menuItem4MouseEntered(e);
                    }
                });
                menu1.add(menuItem4);
            }
            menuBar.add(menu1);
        }
        add(menuBar);
        menuBar.setBounds(new Rectangle(new java.awt.Point(0, 0), menuBar.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JMenuBar menuBar;
    private JMenu menu3;
    private JMenuItem menuItem1;
    private JMenuItem menuItem2;
    private JMenu menu1;
    private JMenuItem menuItem3;
    private JMenuItem menuItem4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
